import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css'

import Element from 'element-ui'

// 数据字典
import dict from './components/Dict'

// 权限指令
import checkPer from '@/utils/permission'
import permission from './components/Permission'
import './assets/styles/element-variables.scss'

// global css
import './assets/styles/index.scss'

import App from './App'
import store from './store'
import router from './router/routers'
import i18n from './lang'
import './assets/icons' // icon
import './router/index' // permission control

Vue.use(checkPer)
Vue.use(permission)
Vue.use(dict)
Vue.use(Element, {
  i18n: (key, value) => i18n.t(key, value)
})
Vue.use(Element, {
  size: Cookies.get('size') || 'small' // set element-ui default size
})

// 用闭包实现局部对象storage(注意Storage的方法都重写一遍，不然调用其对象原型方法会报错。)
var localStorageMock = (function(win) {
  var storage = win.localStorage
  return {
    setItem: function(key, value) {
      var setItemEvent = new Event('setItemEvent')
      var oldValue = storage[key]
      setItemEvent.key = key
      // 新旧值深度判断，派发监听事件
      if (oldValue !== value) {
        setItemEvent.newValue = value
        setItemEvent.oldValue = oldValue
        win.dispatchEvent(setItemEvent)
        storage[key] = value
        return true
      }
      return false
    },
    getItem: function(key) {
      return storage[key]
    },
    removeItem: function(key) {
      storage[key] = null
      return true
    },
    clear: function() {
      storage.clear()
      return true
    },
    key: function(index) {
      return storage.key(index)
    }
  }
}(window))

Object.defineProperty(window, 'localStorage', { value: localStorageMock, writable: true })

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
})
